
var Request = GetRequest();

var isXiao = Request["isXiao"], token = location.search.split("token=")[1];

var flmActionClient = (function () {
    var that = this;
    var callBackStore = {};
    var userAgent = navigator.userAgent;
    var clientType = {
        isFLMClient: userAgent.indexOf("FLM") > -1,
        isIos: !!userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/),
        isAndroid: userAgent.indexOf('Android') > -1 || userAgent.indexOf('Linux') > -1, //android终端或者uc浏览器  
    };
    that.showMessage = function () {

    }
    that.Call = function (url, data, successCallBack, errorCallBack) {
        if (clientType.isFLMClient) {
            that.showMessage("FlmClient 调用Action");
            var callBackName = getCallBackName();
            var message = {
                url: url,
                data: data,
                callBackName: callBackName
            }
            var callBack = function (callBackName, statusCode, result) {
                that.showMessage("FlmClient callBack: " + callBackName + " - " + statusCode);
                if (statusCode >= 200 && statusCode < 300) {
                    that.showMessage("FlmClient callBack:Success" + callBackName + " - " + result);
                    successCallBack(result);
                } else {
                    that.showMessage("FlmClient callBack:Error" + callBackName + " - " + result);
                    errorCallBack(statusCode, result);
                }
            }
            callBackStore[callBackName] = callBack;

            if (clientType.isIos) {
                window.webkit.messageHandlers.ActionClient.postMessage(message);
            }
            else if (clientType.isAndroid)
                window.androidActionClient.Call(url, JSON.stringify(data), callBackName);
            else
                that.showMessage("FlmClient Call:Error");
        }
        else {

            
            //判断是不是在小程序端
            if (isXiao) {


                $.ajax({
                    type: 'POST',
                    url: url,
                    // post payload:
                    data: JSON.stringify(data),
                    contentType: 'application/json',
                    async: false,
                    headers: {
                        "Authorization": "Bearer " + decodeURIComponent(token)
                    },
                    success: function (data) {
                        successCallBack(data);
                    },
                    error: function (xhr, type) {
                        var result = xhr.responseText != "" ? JSON.parse(xhr.responseText) : {};
                        var data = {
                            StatusTag: result.StatusTag ? result.StatusTag : undefined,
                            Message: result.Message ? result.Message : undefined,
                            Result: result.Result ? result.Result : {}
                        };
                        errorCallBack(xhr.status, data)
                    }
                })
            } else {
                $.ajax({
                    type: 'POST',
                    url: url,
                    // post payload:
                    data: JSON.stringify(data),
                    contentType: 'application/json',
                    async: false,
                    headers: {
                        "Authorization": "Bearer " + "dGJi.OOkdiGNbBAKOFJY4lRO6ttrK8jrtunoUJ9dMW6QCKpOawoZmtPoAGfcb2HP1lA6rEcUxq1M5o/ufx9VgVp6JkhUVYP9aYtuFtNR3qdS7lUsTNAqdBs40NxTBGQ8B44YVPIoso3nENKk83HsAf2AkQTjj0MltXDEPzQTtv8iLnFMKxnsKx7D//R7HsUW0iXpS68QvW/mE/vIwcYESINAx/w==","expires_in":28800,"token_type":"Bearer","refresh_token":"dGJi.21GpXGpUHlX97CCHWDCI+6NjwxeECHiRQ8QLSBql1OWLjtKhACHhzFemBPkTjxEYqApd32lz6T1pXywD8RMBZrmIlc+rp7ncrMcWpX5MAYpz/WlzV1eaGjoXOnLF4ITsooz1kivM+4FTxwkd1VQsfJDMYwFXQ19fvLBlHK2ujMPRA0/K2gMqfh1N0jUYCQds/Iz/wTH9qc3lx7NnTaNmaw123VMbsmR/5uLRBM6uCxA="
                    },
                    success: function (data) {
                        successCallBack(data);
                    },
                    error: function (xhr, type) {

                        if (xhr.status != 0) {
                            var result = xhr.responseText != "" ? JSON.parse(xhr.responseText) : {};
                            var data = {
                                StatusTag: result.StatusTag ? result.StatusTag : undefined,
                                Message: result.Message ? result.Message : undefined,
                                Result: result.Result ? result.Result : {}
                            }
                        } 
                        errorCallBack(xhr.status, data)
                    }
                })
            }

            that.showMessage("Http调用Action" + userAgent);

        }




    }

    that.CallBack = function (callBackName, statusCode, result) {
        that.showMessage("调用CallBack:" + callBackName);
        if (clientType.isFLMClient) {
            if (clientType.isAndroid) {
                callBackStore[callBackName](callBackName, statusCode, result !== undefined ? result : {});
                return;
            }
            else if (clientType.isIos) {
                that.showMessage("调用CallBack ios:" + typeof (result));
                try {
                    callBackStore[callBackName](callBackName, statusCode, result !== undefined ? result : {});
                }
                catch (err) {
                    that.showMessage("调用CallBack ios 异常:" + err);
                }
                return;
            }
        }
        that.showMessage("调用 UNCallBack:");

    }
    return that;

    function getCallBackName() {
        return 'callBackxxxxxxxx'.replace(/[xy]/g, function (c) {
            var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
            return v.toString(16);
        });
    }


})()

function GetRequest() {
    var url = location.search; //获取url中"?"符后的字串
    var theRequest = new Object();
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        strs = str.split("&");
        for (var i = 0; i < strs.length; i++) {
            theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
        }
    }
    return theRequest;
}

